package com.kxs.stu_manager.model;

import com.kxs.stu_manager.global.JdbcUtils;
import com.kxs.stu_manager.global.User;
import com.kxs.stu_manager.views.UserPage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName UserModel
 * @Description: TODO 用户的模型层
 * @Author: 2119475835@qq.com
 * @Date: 2023/1/9
 */
public class UserModel {
    private Connection conn = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    /**
     * 进行用户名和密码的校对
     * @param loginUser 用户输入的信息
     * @return 成功 - 返回完整信息的对象 失败 - 返回null
     */
    public User doLogin(User loginUser) throws SQLException {
        User user = null;

        User userTmp = getUserByName(loginUser.getUserName());
        if(null == userTmp){
            UserPage.failed("用户不存在");
        }else{
            if(userTmp.getPassword().equals(loginUser.getPassword())){
                UserPage.success("登录成功！~");
                user = userTmp;
            }else {
                UserPage.failed("密码错误");
            }
        }
        return user;
    }

    /**
     * 封装一个进行数据校对的方法
     * @param userName 要校对的用户名
     * @return 成功 - 返回完整信息的对象 失败 - 返回null
     */
    public User getUserByName(String userName) throws SQLException {
        User user = null;
        conn = JdbcUtils.getConnection();

        String sql = "select `id`, `userName`, `password`, `competence` from `user` where `userName` = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1, userName);
        rs = pst.executeQuery();

        if(rs.next()){
            user = new User(
                rs.getInt("id"),
                rs.getString("userName"),
                rs.getString("password"),
                rs.getString("competence")
            );
        }

        JdbcUtils.free(rs, pst, conn);
        return user;
    }

    /**
     * 将用户输入的信息对象添加到数据库中
     * @param regUser 用户注册的信息
     * @return 成功 - 返回一个全部信息的对象 失败 - 返回null
     */
    public User doReg(User regUser) throws SQLException {
        User user = null;

        conn = JdbcUtils.getConnection();
        String sql = "insert into `user`(`userName`, `password`) value (?,?)";
        pst = conn.prepareStatement(sql);

        pst.setString(1, regUser.getUserName());
        pst.setString(2, regUser.getPassword());

        int rows = pst.executeUpdate();
        if(rows > 0){
            UserPage.success("注册成功！~");
            user = getUserByName(regUser.getUserName());
        }

        JdbcUtils.free(rs, pst, conn);
        return user;
    }

    /**
     * 根据id获取用户信息集合
     * @param userId 用户id
     * @return 用户信息集合
     */
    public ArrayList<User> getUserById(int userId) throws SQLException {
        ArrayList<User> userList = new ArrayList<>();
        conn = JdbcUtils.getConnection();

        String sql = "select `id`, `userName`, `competence` from `user` where `id` = ?";
        pst = conn.prepareStatement(sql);
        pst.setInt(1, userId);

        rs = pst.executeQuery();
        if (rs.next()){
            userList.add(
                    new User(rs.getInt("id"),
                            rs.getString("username"),
                            rs.getString("competence"))
            );
        }

        JdbcUtils.free(rs, pst, conn);

        return userList;
    }

    /**
     * 根据用户名获取用户信息集合
     * @param username 用户名
     * @return 用户信息集合
     */
    public ArrayList<User> getUserInfoByName(String username) throws SQLException {
        ArrayList<User> userList = new ArrayList<>();
        conn = JdbcUtils.getConnection();

        String sql = "select `id`, `userName`, `competence` from `user` where `userName` = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1, username);

        rs = pst.executeQuery();
        if (rs.next()){
            userList.add(
                    new User(rs.getInt("id"),
                            rs.getString("username"),
                            rs.getString("competence"))
            );
        }

        JdbcUtils.free(rs, pst, conn);

        return userList;
    }

    /**
     * 根据id获取用户信息集合
     * @param com 用户权限
     * @return 用户信息集合
     */
    public ArrayList<User> getUserByCompetence(String com) throws SQLException {
        ArrayList<User> userList = new ArrayList<>();
        conn = JdbcUtils.getConnection();

        String sql = "select `id`, `userName`, `competence` from `user` where `competence` = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1, com);

        rs = pst.executeQuery();
        while (rs.next()){
            userList.add(
                    new User(rs.getInt("id"),
                            rs.getString("username"),
                            rs.getString("competence"))
            );
        }

        JdbcUtils.free(rs, pst, conn);

        return userList;
    }

    /**
     * 进行修改权限操作
     * @param user 修改后的对象
     */
    public void updateUser(User user) throws SQLException {
        conn = JdbcUtils.getConnection();

        String sql = "update `user` set `competence` = ? where `userName` = ?";
        pst = conn.prepareStatement(sql);

        pst.setString(1, user.getCompetence());
        pst.setString(2, user.getUserName());

        int rows = pst.executeUpdate();
        if(rows > 0){
            UserPage.success("修改成功！~");
        }else{
            UserPage.failed("修改失败！~");
        }

        JdbcUtils.free(rs, pst, conn);
    }

    /**
     * 进行删除
     * @param username 要删除的用户名
     * @throws SQLException
     */
    public void doDelUser(String username) throws SQLException {
        conn = JdbcUtils.getConnection();

        String sql = "delete from `user` where `userName` = ?";
        pst = conn.prepareStatement(sql);

        pst.setString(1, username);

        int rows = pst.executeUpdate();
        if(rows > 0){
            UserPage.success("删除成功！~");
        }else{
            UserPage.failed("删除失败！~");
        }

        JdbcUtils.free(rs, pst, conn);
    }

    /**
     * 查询全部
     * @return 全部用户信息
     */
    public ArrayList<User> selAllUser() throws SQLException {
        ArrayList<User> userList = new ArrayList<>();
        conn = JdbcUtils.getConnection();

        String sql = "select `id`, `userName`, `competence` from `user`";
        pst = conn.prepareStatement(sql);

        rs = pst.executeQuery();
        while (rs.next()){
            userList.add(
                    new User(rs.getInt("id"),
                            rs.getString("username"),
                            rs.getString("competence"))
            );
        }

        JdbcUtils.free(rs, pst, conn);

        return userList;
    }

    /**
     * 删除全部用户信息
     */
    public void doDelAllUser() throws SQLException {
        conn = JdbcUtils.getConnection();

        String sql = "delete from `user`";
        pst = conn.prepareStatement(sql);

        int rows = pst.executeUpdate();

        if(rows > 0){
            UserPage.success("删除成功！~");
        }else {
            UserPage.failed("删除失败！~");
        }

        JdbcUtils.free(rs, pst, conn);
    }
}
